package Controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
	final String DEFAULT_DESTINATION = "jms/topic/test";
	final String DEFAULT_USERNAME = "XB";//fuckXB
	final String DEFAULT_PASSWORD = "123456";
	final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
	final String PROVIDER_URL = "remote://localhost:4447";
	final Properties env = new Properties();
	private Context context = null;
	private Connection connection = null;

    public UploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    



	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	protected void sendJMS(String[] tube_ids, String status, String time) throws NamingException, JMSException {
	    //初始化Context的工厂类
		env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
	    //Context服务提供者的URL
		env.put(Context.PROVIDER_URL, PROVIDER_URL);
	    //应用用户的登录名,密码.
		env.put(Context.SECURITY_PRINCIPAL, DEFAULT_USERNAME);			
		env.put(Context.SECURITY_CREDENTIALS, DEFAULT_PASSWORD);
		// 获取到InitialContext对象.
		context = new InitialContext(env);
		System.out.println ("获取连接工厂!");
		ConnectionFactory connectionFactory = (ConnectionFactory)context.lookup(DEFAULT_CONNECTION_FACTORY);
		System.out.println ("获取目的地!");
		Destination destination = (Destination)context.lookup(DEFAULT_DESTINATION);
		// 创建JMS连接、会话、生产者和消费者
		connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = session.createProducer(destination);
		connection.start();
		//连接开启
		for (String tube_id : tube_ids) {
			MapMessage msg = session.createMapMessage();
			msg.setString("tube_id", tube_id);
			msg.setString("status", status);
			msg.setString("time", time);
			//发送消息
			producer.send(msg);
		}
		if (context != null) {
			context.close();
		}
		// 关闭连接负责会话,生产商和消费者
		if (connection != null) {
			connection.close();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("upload".equals(method)) {
			String[] tube_ids = request.getParameter("tube_id").split(",");
			String status = request.getParameter("status");
			Date date = new Date();
			SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = dateFormat.format(date);
			
			try {
				sendJMS(tube_ids, status, time);
			} catch (Exception e) {
				e.printStackTrace();
			} 
			
			response.sendRedirect("upload.jsp");
		}
	}

}
